package com.dc.aimc.auth.controller;


import com.dc.aimc.auth.service.AuthService;
import com.dc.aimc.auth.vo.LoginResult;
import com.dc.aimc.common.service.exception.CustomException;
import com.dc.aimc.common.service.response.JsonResponse;
import com.dc.aimc.user.dto.LoginDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@Log4j2
@RestController
@RequestMapping("auth")
@Api(tags = "认证模块")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 普通用户登录
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "普通用户登录认证")
    @PostMapping("login")
    public JsonResponse<Object> login(@RequestBody LoginDTO dto) {
        try {
            LoginResult loginResult = authService.login(dto.getUsername(), dto.getPassword());
            return JsonResponse.success(loginResult);
        } catch (CustomException e) {
            log.error("普通用户登录认证异常: ", e);
            return JsonResponse.fail(e.getCode(), e.getMessage());
        }
    }


    /**
     * 第三方应用登录
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "第三方应用登录认证")
    @PostMapping("appLogin")
    public JsonResponse<Object> appLogin(@RequestBody LoginDTO dto) {
        try {
            LoginResult loginResult = authService.appLogin(dto.getAppId(), dto.getAppSignature(), dto.getTimestamp());
            return JsonResponse.success(loginResult);
        } catch (CustomException e) {
            log.error("第三方应用登录认证异常: ", e);
            return JsonResponse.fail(e.getCode(), e.getMessage());
        }
    }
}
